import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

export const constantRoutes: RouterTypes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401.vue')
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'dashboard',
        meta: { title: '首页', icon: 'example', affix: true }
      }
    ]
  },
  {
    path: '/element',
    component: Layout,
    redirect: '/element/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/element/index.vue'),
        name: 'element',
        meta: { title: '组件', icon: 'list' }
      }
    ]
  },
  {
    path: '/icons',
    component: Layout,
    redirect: '/icons/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/icons/index.vue'),
        name: 'icons',
        meta: { title: '图标', icon: 'icon' }
      }
    ]
  },
  {
    path: '/hook',
    component: Layout,
    redirect: '/hook/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/hook-case/index.vue'),
        name: 'hook-case',
        meta: { title: 'Hook案例', icon: 'nested' }
      }
    ]
  },
  {
    path: '/table',
    component: Layout,
    name: 'table',
    redirect: '/table/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/table/index.vue'),
        name: 'table-crud',
        meta: { title: '表格增删改查', icon: 'table' },
      }
    ]
  }
]

const page404 = { path: '/:catchAll(.*)', name: 'CatchAll', redirect: '/404', hidden: true }

export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/index',
    meta: { title: '权限测试页', icon: 'password', roles: ['admin', 'editor'] },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/permission/page.vue'),
        name: 'permission-page',
        meta: { title: '页面权限', roles: ['admin'] }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive.vue'),
        name: 'permission-directive',
        meta: { title: '指令权限' }
      }
    ]
  },
  page404
]

const router = createRouter({
  history: createWebHashHistory(),
  scrollBehavior: () => ({ top: 0 }),
  routes: constantRoutes
})

export default router